<template>
	<view>
		<!-- 状态栏 -->
		<view class="statusBar" :style="'height:'+statusBarHeight+'px'"></view>
		<view class="myheader padding-sm  flex justify-between">
			<view class="font-sub-title">
				<text class="cuIcon-back margin-right-ssm" @click="back()"></text>
				{{title}}闹钟
			</view>
			<text class="font-sub-title" @click="save()">保存</text>
		</view>
		<picker mode="time" :value="time" @change="bindTimeChange">
			<view class="myTime">{{time}}</view>
		</picker>
		<view class="ul" >
			<view class="li" @click="changeWeek(index)" :class="{ active: str.substring(str.length-1-index,str.length-index) == 1 }" v-for="(item,index) in weekList">
				{{item}}
			</view>
		</view>
		<view class="padding flex flex-direction" @click="del">
			<button class="cu-btn text-red lg">删除</button>
		</view>
			
	</view>
</template>

<script>
	const bandSDK = uni.requireNativePlugin('BandSDK')
	var that;
	export default {
		data() {
			return {
				statusBarHeight: Vue.prototype.StatusBar,
				title:'',
				time:'',
				weekList:['周一','周二','周三','周四','周五','周六','周日'],
				oldtime:'',
				str:'',
			}
		},
		onLoad(e){
			that = this;
			this.title=e.title
			this.oldtime=e.time
			this.time = e.time
			this.repeat = e.repeat
			this.isopen = e.isopen
			this.str = Number(this.repeat).toString(2)
			var len = 8-this.str.length
			if(this.str.length!=8){
				for(var i=0;i<len;i++){
					this.str='0'+this.str;
				}
			}
		},
		methods: {
			del(){
				uni.showModal({
					title: '温馨提示',
					content: '您确定删除该闹钟吗？',
					success: function (res) {
					    if (res.confirm) {
							bandSDK.settingDeleteAlarm({
								startHour:that.oldtime.substring(0,2),
								startMin:that.oldtime.substring(3,5)
							},res=>{
								if(res=='success'){
									uni.showToast({
										"title":"删除成功"
									})
									setTimeout(function(){
										uni.navigateBack()
									},1000)
								}
							})
						}
					}
				})
				
			},
			back(){
				uni.navigateBack();
			},
			bindTimeChange(e){
				this.time = e.target.value
			},
			changeWeek(i){
				var a = this.str[this.str.length-i-1]
				if(a==0||a==undefined){
					a=1
				}else{
					a=0
				}
				this.str = this.str.substring(0,this.str.length-i-1) +a+ this.str.substring(this.str.length-i)
			},
			save(){
				this.repeat = parseInt(this.str,2)
				if(this.title=='编辑'){
					bandSDK.settingModfiyAlarm({
						newType:0,
						startHour:that.oldtime.substring(0,2),
						startMin:that.oldtime.substring(3,5),
						newStartHour:that.time.substring(0,2),
						newStartMin:that.time.substring(3,5),
						newWeekRepeat:that.repeat,
						newDelayTime:0
					},res=>{
						if(res=='success'){
							uni.showToast({
								"title":"保存成功"
							})
							setTimeout(function(){
								uni.navigateBack()
							},1000)
						}
					})
				}else{
					bandSDK.settingAddAlarm({
						type:0,
						startHour:that.time.substring(0,2),
						startMin:that.time.substring(3,5),
						weekRepeat:that.repeat,
						delayTime:0
					},res=>{
						if(res=='success'){
							uni.showToast({
								"title":"保存成功"
							})
							setTimeout(function(){
								uni.navigateBack()
							},1000)
						}
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.ul {
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;
		padding: 30rpx;
		.li {
			padding: 0 10rpx;
			box-sizing: border-box;
			height: 60rpx;
			line-height: 60rpx;
			text-align: center;
			font-size: 28rpx;
			color: #222;
			border-radius: 10rpx;
			margin: 5rpx 0 5rpx 0rpx;
			background: #f4f5f9;
	
			&.active {
				font-weight: 500;
				background: #649df1;
				color:white;
			}
		}
	}
	.myTime{
		font-size: 70rpx;
		text-align: center;
	}
</style>
